// miniprogram/pages/reply/reply.js
const db = wx.cloud.database();
const _ = db.command;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    privateChats: [],
    anonymousChats: []
  },

  onPullDownRefresh: function () {
    this.getAnonymousChats()
    this.getPrivateChats()
    wx.stopPullDownRefresh({
      success: (res) => {},
    })
  },

  getPrivateChats: function () {
    wx.showLoading({
      title: '加载中',
    })
    db.collection("privateChatId").where({
      id: db.RegExp({
        //以openid为关键字进行匹配id
        regexp: app.globalData.openid,
      })
    }).orderBy('createTime', 'desc').get().then(res => {
      //获取列表
      var chatList = []
      var idList = []
      var avatarList = []
      for (var i = 0; i < res.data.length; i++) {
        var id = res.data[i].id + ''
        id = id.replace(app.globalData.openid, '')
        var name = res.data[i].name + ''
        name = name.replace(app.globalData.name, '')
        chatList.push({
          name: name,
          id: id,
          newMsg: res.data[i].newMsg,
        })
      }
      this.setData({
        privateChats: chatList
      })
    }).then(res => {
      wx.hideLoading()
    })
  },

  getAnonymousChats: function () {
    wx.showLoading({
      title: '加载中',
    })
    db.collection("anonymousChatId").where({
      id: db.RegExp({
        //以openid为关键字进行匹配id
        regexp: app.globalData.openid,
      })
    }).orderBy('newMsg', 'desc').orderBy('createTime', 'desc').get().then(res => {
      var chatList = []
      for (var i = 0; i < res.data.length; i++) {
        var id = res.data[i].id + ''
        id = id.replace(app.globalData.openid, '')
        var name = res.data[i].name + ''
        name = name.replace(app.globalData.randomName, '')
        chatList.push({
          name: name,
          id: id,
          newMsg: res.data[i].newMsg
        })
      }
      //console.log(chatList)
      this.setData({
        anonymousChats: chatList
      })
    }).then(res => {
      wx.hideLoading()
    })
  },

  onGroupChat: function () {
    wx.showModal({
      title: '即时聊天室',
      content: '即时聊天室仅非匿名交流，需要获取您的微信公开信息。注意：聊天室每小时清理一次',
      success: res => {
        if (res.confirm) {
          wx.navigateTo({
            url: '../room/room',
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  onPrivateChat: function (event) {
    wx.navigateTo({
      url: '../privateChat/index?id=' + event.currentTarget.dataset.id + '&name=' + event.currentTarget.dataset.name,
    })
  },

  onAnonymousChat: function (event) {
    wx.navigateTo({
      url: '../anonymousChat/index?id=' + event.currentTarget.dataset.id + '&name=' + event.currentTarget.dataset.name,
    })
  },

  onDelete: function (event) {
    wx.showModal({
      title: '是否删除？',
      content: '删除聊天后双方的记录都将被清空，对方将无法接收您的信息',
      cancelColor: 'cancelColor',
      success: res => {
        if (res.confirm) {
          var id1 = event.currentTarget.dataset.id + app.globalData.openid
          var id2 = app.globalData.openid + event.currentTarget.dataset.id
          wx.showLoading({
            title: '正在删除..',
          })
          wx.cloud.callFunction({
            name: 'deleteChat',
            data: {
              id: id1
            },
            complete: res => {
              wx.cloud.callFunction({
                name: 'deleteChat',
                data: {
                  id: id2
                },
                complete: res => {
                  wx.hideLoading()
                  this.getPrivateChats()
                }
              })
            }
          })
        }
      }
    })

  },

  onDeleteAnonymous: function (event) {
    wx.showModal({
      title: '是否删除？',
      content: '删除聊天后双方的记录都将被清空，对方将无法接收您的信息',
      cancelColor: 'cancelColor',
      success: res => {
        if (res.confirm) {
          var id1 = event.currentTarget.dataset.id + app.globalData.openid
          var id2 = app.globalData.openid + event.currentTarget.dataset.id
          wx.showLoading({
            title: '正在删除..',
          })
          wx.cloud.callFunction({
            name: 'deleteChatAnonymous',
            data: {
              id: id1
            },
            complete: res => {
              wx.cloud.callFunction({
                name: 'deleteChatAnonymous',
                data: {
                  id: id2
                },
                complete: res => {
                  wx.hideLoading()
                  this.getAnonymousChats()
                }
              })
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getPrivateChats()
    this.getAnonymousChats()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getPrivateChats()
    this.getAnonymousChats()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})